﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommerceOne.Domains.Commons
{
   public  class SearchBuilder
    {
       private List<FilterGroup> filterGroupList = new List<FilterGroup>();
       private string selectString = string.Empty;
       private string orderString = string.Empty;

       public SearchBuilder AddGroup(FilterGroup filterGroup)
       {
           this.filterGroupList.Add(filterGroup);
           return this;
       }

       public SearchBuilder AddGroupRanger(FilterGroup[] filterGroups)
       {
           this.filterGroupList.AddRange(filterGroups);
           return this;
       }

       public SearchBuilder(string selectString)
       {
           this.selectString = selectString;
       }

       public void OrderBy(string orderString)
       {
           this.orderString = orderString;
       }

       /// <summary>
       /// Returns a <see cref="System.String"/> that represents this instance.
       /// </summary>
       /// <returns>
       /// A <see cref="System.String"/> that represents this instance.
       /// </returns>
       public override string ToString()
       {
           string filterString = string.Join(" OR ", filterGroupList);
           string selectStr = selectString +
                                 (string.IsNullOrEmpty(filterString) ? string.Empty : " WHERE " + filterString) +
                                 (string.IsNullOrEmpty(orderString) ? string.Empty : " ORDER BY " + orderString);
           return selectStr;
       }
    }
}
